package com.voice.core.connection.frame;

import java.nio.ByteBuffer;

public class FrameFactory {

    public static Frame newFrame(ByteBuffer buffer){
        if(buffer.remaining()<Frame.headerLength) return null;
        byte[] payload=new byte[Frame.headerLength];
        buffer.get(payload);
        Frame frame=getFrame(payload[3]);
        if(frame!=null) frame.readHeader(payload);
        return frame;
    }

    public static Frame getFrame(byte type){
        switch (type){
            case 0:
                return new DataFrame();
            case 1:
                return new HeaderFrame();
            case 6:
                return new PingFrame();
            case 7:
                return new GoAwayFrame();
            case 10:
                return new StreamFrame();

            default:
                return null;
        }
    }
}
